package xyz.kongbai.shopseckill.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import xyz.kongbai.shopseckill.service.SeckillService;
import xyz.kongbai.shopseckill.to.SeckillSkuRedisTo;
import xyz.kongbai121.common.utils.R;

import java.util.List;

@Controller
public class SeckillController {

    @Autowired
    SeckillService seckillService;

    @ResponseBody
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus(){

        List<SeckillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();
        return R.ok().setData(vos);
    }

    @ResponseBody
    @GetMapping("/sku/skuSeckillInfo/{skuId}")
    public R skuSeckillInfo(@PathVariable("skuId") Long skuId){
        SeckillSkuRedisTo vos = seckillService.skuSeckillInfo(skuId);
        return R.ok().setData(vos);
    }

    @GetMapping("/kill")
    public String secKill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model){

        String orderSn = seckillService.kill(killId,key,num);
        model.addAttribute("orderSn", orderSn);
        return "success";
    }
}
